package com.example.sword;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Test60 {

    public static void main(String[] args) {
        System.out.println("haha");
    }

    public class TreeNode {
        int val;
        TreeNode left = null;
        TreeNode right = null;

        TreeNode(int val) {
            this.val = val;
        }


    }


    /**
     *
     * 从上到下从左到右打印二叉树
     *
     */

    public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {

        ArrayList<ArrayList<Integer>> res = new ArrayList<>();

        if (pRoot==null)return res;
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(pRoot);

        while (!queue.isEmpty()){
            ArrayList<Integer> temp = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode currentNode = queue.poll();
                temp.add(currentNode.val);
                if (currentNode.left!=null)queue.offer(currentNode.left);
                if (currentNode.right!=null)queue.offer(currentNode.right);
            }
            res.add(temp);
        }

        return res;
    }
}
